<?php
namespace App\Services\Index;
use App\Contracts\Index\SearchContracts;
use App\Models\Product;

class SearchService implements SearchContracts
{
    protected $builder;
    //模型构造器
    public function __construct()
    {
        $this->builder = Product::where('on_sale',true);
    }

    public function orderby($orderby){
        if(preg_match('/^(.)_(asc|desc)is/',$orderby,$m)){
            if(in_array($m[1],['price','sold_count'])){
                $this->builder->orderBy($m[1],$m[2]);
                return $this;
            }
        }
    }

    public function search($search)
	{
        $like = '%'.$search.'%';
		$this->builder->where($like);
        return $this;
	}

    public function getproduct($user_id=null){
        return $this->builder->paginate(15);
    }
}
